package com.mvc.handler;


import com.mvc.entity.Dog;
import com.mvc.entity.Type;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wzm
 * @create 2023-04-11 11:40
 */
//自定义的结果集处理器
public class DogHandler<T> implements ResultSetHandler<List<T>> {

    //处理器执行操作的方法
    @Override
    public List<T> handle(ResultSet rs) throws SQLException {

      //手动处理查询到的结果
        Dog dog = null;
        List<Dog> list = new ArrayList<>();

        while(rs.next()){
            dog =  new Dog();
            //查询到的dog的信息 赋值
            dog.setId(rs.getInt(1));
            dog.setName(rs.getString(2));
            dog.setAge(rs.getInt(3));
            dog.setSex(rs.getString(4));
            dog.setTypeId(rs.getInt(5));

            //类型相关的东西
            Type type = new Type();
            type.setTypeid(rs.getInt(5));
            type.setTypename(rs.getString(6));
            //将type对象赋值给dog的type属性
            dog.setType(type);
            list.add(dog);
            
        }

        
        return (List<T>) list;
    }
}
